
  <?php echo $this->user->messages(); ?>

<?php 
$rows = $this->db->query("select * from meterial_base_categories order by sort_order asc")->result_array();
foreach($rows as $base){
	$type_condition = "";
	if($type_id)
	{
		$type_condition = " and type_id =".$type_id;
	}
$sql ="SELECT
material_categories.category_name,
material_categories.cat_base_id,
material_categories.category_id,


(select  COALESCE(sum( amount_total  ),0 ) from inventory_views where material_categories.category_id =  inventory_views.category_id ) as total
,(select  COALESCE(sum( allocation_total  ),0 ) from inventory_views where material_categories.category_id =  inventory_views.category_id ) as allocation_total

FROM
material_categories where cat_base_id = ".$base['cat_base_id'].$type_condition;
$cats =  $this->db->query($sql)->result_array(); ?>
<table class="list" >
          <tbody><tr class=" odd">
            <th style="width:300px;" ><?php echo $base["cat_base_name"]; ?></th>
            
            <th style="width:100px;">จำนวนที่มี</th>
            <th style="width:100px;">จัดสรรแล้ว</th>
             <th style="width:100px;">คงเหลือ</th>
          </tr>
         <?php
          $i = 1;
          foreach($cats as $row){ ?>
        
          <tr class="<?php if($i++%2){ echo "even";} ?>" >
            
            
            <td><?php echo $row['category_name']; ?></td>
            
            <td><?php echo $row['total']; ?></td>
             <td><?php echo $row['allocation_total']; ?></td>
            <td><?php echo ($row['total'] - $row['allocation_total']); ?></td>
          </tr>
          <?php } } ?>
        </tbody></table>